﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace Auktionshuset
{
    public delegate void Beregner(int varenr, string navn, string beskrivelse, string varensstand, string billede, decimal mindstePris);

    public class Produkt : Auktionshuset.IProdukt1
    {
        Socket klient;
        private const int PORT = 12345;
        private IPEndPoint serveradr;

        public Produkt()
        {
            serveradr = new IPEndPoint(IPAddress.Loopback, PORT);
        }

        public event Beregner BeregnerEvent;

        //public string Beskrivelse
        //{ get { return ""; } set { value = ""; } }

        //public string Billede { get; set; }
        //public decimal MindstePris { get; set; }
        //public string Navn { get; set; }
        //public int Varenr { get; set; }
        //public string VarensStand { get; set; }

        public string Navn
        {
            get
            {
                string navn;
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                server.Connect(serveradr);

                NetworkStream networkStream = new NetworkStream(server);
                StreamWriter streamWriter = new StreamWriter(networkStream);
                StreamReader streamReader = new StreamReader(networkStream);

                streamWriter.WriteLine("HentNavn");
                streamWriter.Flush();
                navn = streamReader.ReadLine();

                streamWriter.Close();
                streamReader.Close();
                networkStream.Close();
                server.Shutdown(SocketShutdown.Both);
                server.Close();

                return navn;
            }
        }
        public string Beskrivelse
        {
            get
            {
                string beskrivelse;
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                server.Connect(serveradr);

                NetworkStream networkStream = new NetworkStream(server);
                StreamWriter streamWriter = new StreamWriter(networkStream);
                StreamReader streamReader = new StreamReader(networkStream);

                streamWriter.WriteLine("HentBeskrivelse");
                streamWriter.Flush();
                beskrivelse = streamReader.ReadLine();

                streamWriter.Close();
                streamReader.Close();
                networkStream.Close();
                server.Shutdown(SocketShutdown.Both);
                server.Close();

                return beskrivelse;
            }
        }

        public string Varestand
        {
            get
            {
                string varestand;
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                server.Connect(serveradr);

                NetworkStream networkStream = new NetworkStream(server);
                StreamWriter streamWriter = new StreamWriter(networkStream);
                StreamReader streamReader = new StreamReader(networkStream);

                streamWriter.WriteLine("HentVarestand");
                streamWriter.Flush();
                varestand = streamReader.ReadLine();

                streamWriter.Close();
                streamReader.Close();
                networkStream.Close();
                server.Shutdown(SocketShutdown.Both);
                server.Close();

                return varestand;
            }
        }

        public string Billed
        {
            get
            {
                string billed;
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                server.Connect(serveradr);

                NetworkStream networkStream = new NetworkStream(server);
                StreamWriter streamWriter = new StreamWriter(networkStream);
                StreamReader streamReader = new StreamReader(networkStream);

                streamWriter.WriteLine("HentBilled");
                streamWriter.Flush();
                billed = streamReader.ReadLine();

                streamWriter.Close();
                streamReader.Close();
                networkStream.Close();
                server.Shutdown(SocketShutdown.Both);
                server.Close();

                return billed;
            }
        }

        public decimal MindstePris
        {
            get
            {
                decimal mindstePris;
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                server.Connect(serveradr);

                NetworkStream networkStream = new NetworkStream(server);
                StreamWriter streamWriter = new StreamWriter(networkStream);
                StreamReader streamReader = new StreamReader(networkStream);

                streamWriter.WriteLine("HentMindstePris");
                streamWriter.Flush();
                mindstePris = decimal.Parse(streamReader.ReadLine());

                streamWriter.Close();
                streamReader.Close();
                networkStream.Close();
                server.Shutdown(SocketShutdown.Both);
                server.Close();

                return mindstePris;
            }
        }

        public decimal SidsteBud
        {
            get
            {
                decimal sidsteBud;
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                server.Connect(serveradr);

                NetworkStream networkStream = new NetworkStream(server);
                StreamWriter streamWriter = new StreamWriter(networkStream);
                StreamReader streamReader = new StreamReader(networkStream);

                streamWriter.WriteLine("HentSidsteBud");
                streamWriter.Flush();
                sidsteBud = decimal.Parse(streamReader.ReadLine());

                streamWriter.Close();
                streamReader.Close();
                networkStream.Close();
                server.Shutdown(SocketShutdown.Both);
                server.Close();

                return sidsteBud;
            }
            set
            {
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                server.Connect(serveradr);
                NetworkStream networkStream = new NetworkStream(server);
                StreamReader streamReader = new StreamReader(networkStream);
                StreamWriter streamWriter = new StreamWriter(networkStream);

                streamWriter.WriteLine("RetSidsteBud");
                streamWriter.WriteLine(value.ToString());
                streamWriter.Flush();

                server.Shutdown(SocketShutdown.Both);
                server.Close();
                streamReader.Close();
                streamWriter.Close();
            }
        }

        public decimal KlientSidsteBud
        {
            get
            {
                decimal klientSidsteBud;
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                server.Connect(serveradr);

                NetworkStream networkStream = new NetworkStream(server);
                StreamWriter streamWriter = new StreamWriter(networkStream);
                StreamReader streamReader = new StreamReader(networkStream);

                streamWriter.WriteLine("HentKlientsSidsteBud");
                streamWriter.Flush();
                klientSidsteBud = decimal.Parse(streamReader.ReadLine());

                streamWriter.Close();
                streamReader.Close();
                networkStream.Close();
                server.Shutdown(SocketShutdown.Both);
                server.Close();

                return klientSidsteBud;
            }
            set
            {
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                server.Connect(serveradr);
                NetworkStream networkStream = new NetworkStream(server);
                StreamReader streamReader = new StreamReader(networkStream);
                StreamWriter streamWriter = new StreamWriter(networkStream);

                streamWriter.WriteLine("RetKlientsSidsteBud");
                streamWriter.WriteLine(value.ToString());
                streamWriter.Flush();

                server.Shutdown(SocketShutdown.Both);
                server.Close();
                streamReader.Close();
                streamWriter.Close();
            }
        }

        public int VareNr
        {
            get
            {
                int vareNr;
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                server.Connect(serveradr);

                NetworkStream networkStream = new NetworkStream(server);
                StreamWriter streamWriter = new StreamWriter(networkStream);
                StreamReader streamReader = new StreamReader(networkStream);

                streamWriter.WriteLine("HentVareNr");
                streamWriter.Flush();
                vareNr = int.Parse(streamReader.ReadLine());

                streamWriter.Close();
                streamReader.Close();
                networkStream.Close();
                server.Shutdown(SocketShutdown.Both);
                server.Close();

                return vareNr;
            }
        }

        public decimal Byd(decimal beløb)
        {
               decimal sidsteBud;

            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            server.Connect(serveradr);

            NetworkStream networkstream = new NetworkStream(server);
            StreamReader streamReader = new StreamReader(networkstream);
            StreamWriter streamwriter = new StreamWriter(networkstream);

            streamwriter.WriteLine("Byd");
            streamwriter.WriteLine(beløb.ToString());
            streamwriter.Flush();
            sidsteBud = Convert.ToDecimal(streamReader.ReadLine());

            //streamwriter.WriteLine("RetKlientsSidsteBud");
            //streamwriter.WriteLine(beløb.ToString());
            //streamwriter.Flush();

            streamwriter.Close();
            networkstream.Close();
            server.Shutdown(SocketShutdown.Both);
            server.Close();

            return sidsteBud;
        }

        public string HentIp(decimal klientsBud, decimal sidsteBud)
        {
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            string data;

            server.Connect(serveradr);

            NetworkStream networkStream = new NetworkStream(server);
            StreamReader streamReader = new StreamReader(networkStream);
            StreamWriter streamWriter = new StreamWriter(networkStream);

            streamWriter.WriteLine("HentIp");
            streamWriter.WriteLine(klientsBud);
            streamWriter.Flush();

            data = streamReader.ReadLine();
            
            streamWriter.Close();
            networkStream.Close();
            server.Shutdown(SocketShutdown.Both);
            server.Close();

            return data;
        }
    }
}
